package com.rainner.admin.server;

import java.sql.SQLException;
import java.util.List;

import com.rainner.admin.javabean.ReaderBean;
import com.rainner.admin.util.dbutil.BaseService;

public class ReaderServer extends BaseService<ReaderBean>{

	/**
	 * 返回一页读者信息
	 * @param page
	 * @param rows
	 * @return
	 * @throws SQLException
	 */
	public List<ReaderBean> allReaderData(long page,long rows) throws SQLException{
		String sqlSelect = "select * from Reader ORDER BY readerDate limit ?,?";
		return super.query(sqlSelect,(page - 1) * rows,rows);
	}
	/**
	 * 返回读者信息总条数
	 * @return
	 * @throws SQLException
	 */
	public long countReaderData() throws SQLException{
		String sqlSelect = "select COUNT(1) from Reader";
		return super.counts(sqlSelect);
	}
	
	/**
	 * 删除选中的读者信息
	 * @param reader
	 * @return
	 * @throws SQLException
	 */
	public boolean delReaderData(ReaderBean reader) throws SQLException{
		String sqlSelect = "select COUNT(1) from Reader where ReaderUsername = ?";
		if(super.counts(sqlSelect, reader.getReaderUsername()) < 1){
			return false;
		}else{
			String sqlDelete = "delete from BrrowedBook where ReaderUsername = ?";
			super.update(sqlDelete, reader.getReaderUsername());
			sqlDelete = "delete from Reader where ReaderUsername = ?";
			return super.update(sqlDelete, reader.getReaderUsername());
		}
	}
	/**
	 * 返回欠费读者信息
	 * @param page
	 * @param rows
	 * @return
	 * @throws SQLException
	 */
	public List<ReaderBean> oweReaderData(long page,long rows) throws SQLException{
		String sqlSelect;
		if(page ==0 && rows == 0){
			sqlSelect ="select * from Reader where ReaderMoney <= -5 ORDER BY readerDate";
			return super.query(sqlSelect);
		}else{
			sqlSelect ="select * from Reader where ReaderMoney <= -5 ORDER BY readerDate limit ?,?";
			return super.query(sqlSelect,(page - 1) * rows,rows);
		}
	}
	
	/**
	 * 返回欠费读者信息总条数
	 * @return
	 * @throws SQLException
	 */
	public long countOweReaderData() throws SQLException{
		String sqlSelect = "select COUNT(1) from Reader where ReaderMoney <= -5";
		return super.counts(sqlSelect);
	}
	
	/**
	 * 修改读者的金额
	 * @param readerUsername
	 * @param money
	 * @return
	 * @throws SQLException
	 */
	public boolean updateReaderMoney(String readerUsername,double money) throws SQLException{
		String sqlSelect = "select * from Reader where ReaderUsername = ?";
		List<ReaderBean> bean = super.query(sqlSelect, readerUsername);
		String sqlUpdate = "update Reader set ReaderMoney = ? where ReaderUsername = ?";
		return super.update(sqlUpdate, bean.get(0).getReaderMoney() + money,readerUsername);
	}
}
